package com.demo.controller;

import cn.hutool.core.convert.Convert;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.demo.entity.Vo.*;
import com.demo.entity.from.DvtScreeningExcle;
import com.demo.entity.from.PatientExcle;
import com.demo.entity.from.VtePreventionExcle;
import com.demo.enums.ExportInfoTypeEnum;
import com.demo.service.ExportDataService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RequestMapping("/export")
@RestController
public class ExportPatientDataController {
    @Autowired
    ExportDataService exportDataService;

    @PostMapping("/patientExport")
    @ApiOperation(value = "数据导出", notes = "数据导出")
    public void patientExport(HttpServletResponse response, @RequestBody ExportVo exportVo) throws IOException {
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String fileName1 = "病患信息报表" + date;
        String fileName2 = "VTE防治评估信息报表" + date;
        String fileName3 = "DVT筛查诊断信息报表" + date;
        String fileName4 = "出院总结信息报表" + date;

        log.info("请求参数---" + exportVo);
        Integer[] types = exportVo.getType();
        List<Integer> integers = Arrays.asList(types);
        PatientExcle patientExcle = exportVo.getPatientExcle();
        VtePreventionExcle vtePreventionExcle = exportVo.getVtePreventionExcle();
        DvtScreeningExcle dvtScreeningExcle = exportVo.getDvtScreeningExcle();

        Integer patientType = null;
        Integer vteType = null;
        Integer dvtType = null;
        Integer dischargedType = null;

        for (Integer type : types) {
            if (type.equals(1)) {
                patientType = type;
            }
            if (type.equals(2)) {
                vteType = type;
            }
            if (type.equals(3)) {
                dvtType = type;
            }
            if (type.equals(4)) {
                dischargedType = type;
            }
        }

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        try {
            String fileName = URLEncoder.encode("数据导出", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //新建ExcelWriter
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();

            if (integers.contains(ExportInfoTypeEnum.PATIENT_INFO)) {
                //获取sheet0对象
                Object object = exportDataService.patientExport(response, ExportInfoTypeEnum.PATIENT_INFO, patientExcle, vtePreventionExcle, dvtScreeningExcle).getData();
                List<PatientExportVo> patientExportVos = Convert.convert(List.class, object);
                WriteSheet mainSheet = EasyExcel.writerSheet(0, fileName1).head(PatientExportVo.class).build();
                //向sheet0写入数据
                excelWriter.write(patientExportVos, mainSheet);
            }


            if (vteType != null) {
                //获取sheet1对象
                Object object = exportDataService.patientExport(response, ExportInfoTypeEnum.VTE_INFO, patientExcle, vtePreventionExcle, dvtScreeningExcle).getData();
                List<VtePreventiveExportVo> vtePreventiveExportVos = Convert.convert(List.class, object);
                WriteSheet detailSheet = EasyExcel.writerSheet(1, fileName2).head(VtePreventiveExportVo.class).build();
                //向sheet1写入数据
                excelWriter.write(vtePreventiveExportVos, detailSheet);
            }

            if (dvtType != null) {
                //获取sheet2对象
                Object object = exportDataService.patientExport(response, ExportInfoTypeEnum.DVT_INFO, patientExcle, vtePreventionExcle, dvtScreeningExcle).getData();
                List<DvtDiagonseExportVo> diagonseExportVos = Convert.convert(List.class, object);
                WriteSheet detailSheet = EasyExcel.writerSheet(2, fileName3).head(DvtDiagonseExportVo.class).build();
                //向sheet2写入数据
                excelWriter.write(diagonseExportVos, detailSheet);
            }

            if (dischargedType != null) {
                //获取sheet3对象
                Object object = exportDataService.patientExport(response, ExportInfoTypeEnum.OUT_HOSPITAL_INFO, patientExcle, vtePreventionExcle, dvtScreeningExcle).getData();
                List<DischargeSummaryExportVo> summaryExportVos = Convert.convert(List.class, object);
                WriteSheet detailSheet = EasyExcel.writerSheet(3, fileName4).head(DischargeSummaryExportVo.class).build();
                //向sheet3写入数据
                excelWriter.write(summaryExportVos, detailSheet);
            }

            //关闭流
            excelWriter.finish();
        } catch (IOException e) {
            log.info(e.getMessage());
        }

    }


}
